/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GeographicParametersComponent.java
 *
 * Created on 4/Dez/2011, 19:43:22
 */

package intopo.evogame.partnerselection.tools.config;


import intopo.evogame.partnerselection.config.TextConfigFilePlainPlayersEditor;
import intopo.evogame.partnerselection.population.GeographicPopulation;
import intopo.evogame.partnerselection.population.Node;
import intopo.util.Util;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JRadioButton;

/**
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 4/Dez/2011
 */
public class GeographicParametersComponent
	extends javax.swing.JPanel
{
	private TextConfigFilePlainPlayersEditor.GeographicPopulationParameters parameters;

	/** Creates new form GeographicParametersComponent */
	public GeographicParametersComponent ()
	{
		System.err.println ("GeographicParametersComponent default constructor");
	}
//	public GeographicParametersComponent (Object o)
//	{
//		System.err.println ("GeographicParametersComponent default constructor");
//	}
	GeographicParametersComponent (TextConfigFilePlainPlayersEditor configFile)
	{
		this.parameters = configFile.getPopulation ();
		initComponents ();
		geometryRadioButtons = new JRadioButton [GeographicPopulation.Geometry.values ().length];
		int i = 0;
		for (GeographicPopulation.Geometry g : GeographicPopulation.Geometry.values ()) {
			this.geometryRadioButtons [i] = new JRadioButton (g.description);
			this.geometryRadioButtons [i].addActionListener (new ActionListener () {
				@Override
				public void actionPerformed (ActionEvent e) {
					geometryRadioButtonActionPerformed (e);
				}
			});
			this.geometryButtonGroup.add (this.geometryRadioButtons [i]);
			this.geometryPanel.add (this.geometryRadioButtons [i]);
			i++;
		}
		this.geometryRadioButtons [0].setSelected (true);
	}

	/**
	 * Update the components after a configuration file has been read.
	 */
	void updateComponents ()
	{
		this.migrationProbabilityFormattedTextField.setValue (new Float (Node.Parameters.migrationProbability));
		this.widthFormattedTextField.setValue (new Integer (this.parameters.getWidth ()));
		this.heightFormattedTextField.setValue (new Integer (this.parameters.getHeight ()));
		this.boundaryCheckBox.setSelected (this.parameters.isBounded ());
		this.geometryRadioButtons [this.parameters.getGeometry ().ordinal ()].setSelected (true);
	}
	private void geometryRadioButtonActionPerformed (ActionEvent e)
	{
		for (int i = this.geometryRadioButtons.length - 1; i >= 0; i--) {
			if (e.getSource () == this.geometryRadioButtons [i]) {
				this.parameters.setGeometry (GeographicPopulation.Geometry.values () [i]);
				return ;
			}
		}
	}

	JRadioButton[] geometryRadioButtons;
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      geometryButtonGroup = new javax.swing.ButtonGroup();
      javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
      migrationProbabilityFormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
      widthFormattedTextField = new javax.swing.JFormattedTextField();
      javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
      heightFormattedTextField = new javax.swing.JFormattedTextField();
      boundaryCheckBox = new javax.swing.JCheckBox();
      geometryPanel = new javax.swing.JPanel();

      setBorder(javax.swing.BorderFactory.createTitledBorder("geographic population"));
      setLayout(new java.awt.GridBagLayout());

      jLabel1.setText("migration probability: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      add(jLabel1, gridBagConstraints);

      migrationProbabilityFormattedTextField.setColumns(5);
      migrationProbabilityFormattedTextField.setValue(new Float (Node.Parameters.migrationProbability));
      migrationProbabilityFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            migrationProbabilityFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      add(migrationProbabilityFormattedTextField, gridBagConstraints);

      jLabel2.setText("width: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      add(jLabel2, gridBagConstraints);

      widthFormattedTextField.setColumns(5);
      widthFormattedTextField.setValue(new Integer (this.parameters.getWidth ()));
      widthFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            widthFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      add(widthFormattedTextField, gridBagConstraints);

      jLabel3.setText("height: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      add(jLabel3, gridBagConstraints);

      heightFormattedTextField.setColumns(5);
      heightFormattedTextField.setValue(new Integer (this.parameters.getHeight ()));
      heightFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            heightFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      add(heightFormattedTextField, gridBagConstraints);

      boundaryCheckBox.setText("boundary");
      boundaryCheckBox.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            boundaryCheckBoxActionPerformed(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridwidth = 2;
      gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
      add(boundaryCheckBox, gridBagConstraints);

      geometryPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("geometry"));
      geometryPanel.setLayout(new javax.swing.BoxLayout(geometryPanel, javax.swing.BoxLayout.Y_AXIS));
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.gridwidth = 2;
      gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
      add(geometryPanel, gridBagConstraints);
   }// </editor-fold>//GEN-END:initComponents

	 private void migrationProbabilityFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_migrationProbabilityFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float newValue = (Float) this.migrationProbabilityFormattedTextField.getValue ();
			final boolean ok = newValue >= 0 && newValue <= 1;
			if (ok) {
				Node.Parameters.migrationProbability = newValue;
			}
			Util.update (this.migrationProbabilityFormattedTextField, ok);
		}
	 }//GEN-LAST:event_migrationProbabilityFormattedTextFieldPropertyChange

	 private void widthFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_widthFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			int newValue = (Integer) this.widthFormattedTextField.getValue ();
			try {
				this.parameters.setWidth (newValue);
				this.firePropertyChange ("population", null, null);
				Util.update (this.widthFormattedTextField, true);
			}
			catch (IllegalArgumentException e) {
				Util.update (this.widthFormattedTextField, false);
			}
		}
}//GEN-LAST:event_widthFormattedTextFieldPropertyChange

	 private void heightFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_heightFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			int newValue = (Integer) this.heightFormattedTextField.getValue ();
			try {
				this.parameters.setHeight (newValue);
				this.firePropertyChange ("population", null, null);
				Util.update (this.heightFormattedTextField, true);
			}
			catch (IllegalArgumentException e) {
				Util.update (this.heightFormattedTextField, false);
			}
		}
}//GEN-LAST:event_heightFormattedTextFieldPropertyChange

	 private void boundaryCheckBoxActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boundaryCheckBoxActionPerformed
		this.parameters.setBounded (this.boundaryCheckBox.isSelected ());
	 }//GEN-LAST:event_boundaryCheckBoxActionPerformed


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JCheckBox boundaryCheckBox;
   private javax.swing.ButtonGroup geometryButtonGroup;
   private javax.swing.JPanel geometryPanel;
   private javax.swing.JFormattedTextField heightFormattedTextField;
   private javax.swing.JFormattedTextField migrationProbabilityFormattedTextField;
   private javax.swing.JFormattedTextField widthFormattedTextField;
   // End of variables declaration//GEN-END:variables

}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
